package com.bytedance.frameworks.baselib.network.http.b.a;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.reflect.Reflect;

import java.io.IOException;
import java.net.InetSocketAddress;

import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.Request;
import okhttp3.Route;
import okhttp3.internal.connection.RealConnection;
import okhttp3.Connection;

final class b implements Interceptor {
    b() {

    }

    public final Response intercept(RealInterceptorChain arg8) throws IOException {
        Response v0_2;
        InetSocketAddress v1_2 = null;
        Route v1_1;
        RealConnection v3;
        Object v0 = null;
        Request v2 = arg8.request();
        try {
            v3 = arg8.connection();
            if (v3 != null) {
                v1_1 = ((Connection) v3).route();
            } else {
                v1_1 = ((Route) v0);
            }
            if (v1_1 != null) {
                v0 = v1_1.inetSocketAddress;
            }

            if (Logger.debug()) {
                Logger.d("OkHttp3Builder", "-call- get res -  req: " + v2.hashCode() + " conn: " + v3 + " route: " + v1_1 + " addr: " + v0);
            }
            v1_2 = ((InetSocketAddress) v0);
            v0_2 = arg8.proceed(v2);
            if (v1_2 == null) {
                return v0_2;
            }
        } catch (IOException v2_1) {
            if (v1_2 != null) {
                try {
                    String v0_4 = v2_1.getMessage();
                    StringBuilder v3_1 = new StringBuilder();
                    StringBuilder v1_3 = v3_1.append(v1_2.getAddress().getHostAddress()).append("|");
                    if (v0_4 == null) {
                        v0_4 = "null";
                    }

                    v1_3.append(v0_4);
                    Reflect.on(v2_1).set("detailMessage", v3_1.toString());
                } catch (Throwable v0_3) {
                    v0_3.printStackTrace();
                }
            }

            throw v2_1;
        }

        try {
            Response.Builder v2_2 = v0_2.newBuilder();
            v2_2.addHeader("x-snssdk.remoteaddr", v1_2.getAddress().getHostAddress());
            v0_2 = v2_2.build();
        } catch (Throwable v1) {
        }

        return v0_2;
    }
}

